/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xsengine;

import com.xsengine.db.DBOperator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import static com.baga.util.Print.*;

/**
 *
 * @author baga
 */
public class ContextResemblance {

    private Vector crVals;
    private boolean stopList = false;
    private boolean stemming = false;

    public ContextResemblance() {
        crVals = new Vector();
    }

    public ContextResemblance(boolean isStoplist, boolean isStemming) {
        crVals = new Vector();
    }

    public double getCR(String token, String dict) {
        double crValue = 0.0;
        boolean testMatch = true;
        //String[] normalTokens = SplitNormalizeToken.getNormalTokens(token);
        String[] normalTokens = token.split(" ");
        String[] tempString = dict.split("#");
        String leafNodes = tempString[1];
        tempString = tempString[0].trim().split("/");
        String[] loopString = new String[tempString.length + 1];
        for (int i = 0; i < tempString.length; i++) {
            loopString[i] = tempString[i];
        }

        loopString[tempString.length] = leafNodes;
        for (String tokenString : normalTokens) {
            boolean testInside = false;
            for (String mathcString : loopString) {
                if (mathcString.equalsIgnoreCase(tokenString)) {
                    testInside = true;
                }
            }
            if (testInside) {
                break;
            }
            testMatch &= testInside;
        }

        if (testMatch) {
            crValue = (1.0 + normalTokens.length) / (1.0 + tempString.length);
        }

        return crValue;
    }

    public String[] getCQ(String term, String[] dict) {
        Vector tempVect = new Vector();
        for (String tmpStr : dict) {
            String contDict = tmpStr.substring(0, tmpStr.indexOf("#"));
            String[] arrContDict = contDict.split("/");
            for (String tmpArrContDict : arrContDict) {
                if (term.equalsIgnoreCase(tmpArrContDict)) {
                    tempVect.add(tmpStr);
                }
                break;
            }
        }
        return (String[]) tempVect.toArray();
    }

    public HashMap crHash(String query, String[] dictionary) {
        HashMap hm = new HashMap();
        String[] queryBuilt = query.split(" ");
        for (int i = 0; i < dictionary.length; i++) {
            double crVal = getCR(query, dictionary[i]);
            if (crVal > 0.0) {
                hm.put(dictionary[i], crVal);
            }
        }
        return hm;
    }

    /*
     * fungsi ini mirip dengan fungsi crHash tetapi menggunakan array string
     * dan untuk pembobotannya menggunakan array double
     * input :
     * (String, String[])
     * output :
     * String[]
     */
    public String[] getStrCR(String query, String[] dictionary) {
        ArrayList tempVector = new ArrayList();
        String[] queryBuilt = query.split(" ");
        String[] tmpStr = query.split(" ");
        for (int i = 0; i < dictionary.length; i++) {
            for (int j=0; j<tmpStr.length; j++)  {
                double crVal = getCR(tmpStr[j], dictionary[i]);
                if (crVal > 0.0) {
                    tempVector.add(dictionary[i]);
                    crVals.add(crVal);
                }
            }
        }
        return (String[]) tempVector.toArray(new String[tempVector.size()]);
    }

    /*
     * fungsi ini sebagai nilai dari cr
     * input :
     * void
     * output :
     * Double[]
     */
    public Double[] getCRVal() {
        if (crVals.isEmpty() || crVals == null) {
            return null;
        } else {
            return (Double[]) crVals.toArray(new Double[crVals.size()]);
        }
    }

//    public static void main(String[] args) {
//        long mill = System.currentTimeMillis();
//        DBOperator dbOp = new DBOperator();
//        String[] testOp = dbOp.getAllDoc("dic_post");
//        ContextResemblance cr = new ContextResemblance();
//        /*
//        String[] crVal = cr.crArray("catalog harold", testOp);
//        for(String pr : crVal) {
//        print(pr);
//        }
//         * */
//        String[] hm = cr.getStrCR("rejection by prince norodom sihanouk, an asian neutralist leader, of all further U.S. aid to his nation", testOp);
//        System.out.println(hm.length);
//        Double[] hmVal = cr.getCRVal();
////        double test = cr.getCR("payload data", "/ta_pa/abstract#payload#101");
////        System.out.println(test);
//        for (int i = 0; i < hm.length; i++) {
//            print("Contextnya " + hm[i] + " nilai nya " + hmVal[i]);
//        }
//
//        mill = (System.currentTimeMillis() - mill) / 1000;
//        print(mill);
//    }
}
